package model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
 * A class used to manage all game entities.
 * @author N. Bhatia, A. Butt, N. Evans, C. Levesque, P. OKeefe
 *
 */
public class GameEntityManager {
	private static GameEntityManager instance;
	private Collection<GameEntity> entities;

	private GameEntityManager() {
		entities = new ArrayList<GameEntity>();
	}

	/**
	 * Singleton GameEntityManager used to return the current instance of the GameEntityManager
	 * @return The current instance of the GameEntityManager
	 */
	protected static GameEntityManager Instance() {
		if (instance == null) {
			instance = new GameEntityManager();
		}
		return instance;
	}

	/**
	 * Registers an entity with the game manager.
	 * @param entity The entity to register.
	 */
	public void registerGameEntity(GameEntity entity) {
		this.entities.add(entity);
	}

	/**
	 * Unregisters an entity with the game manager.
	 * @param entity
	 */
	public void unregisterGameEntity(GameEntity entity) {
		this.entities.remove(entity);
	}

	/**
	 * Updates the registered entities.
	 * @param elapsedTimeInMillis
	 */
	public void update(int elapsedTimeInMillis) {
		for (Iterator<GameEntity> iter = this.entities.iterator(); iter
				.hasNext();) {
			GameEntity entity = iter.next();
			entity.preUpdate();
			entity.update(elapsedTimeInMillis);
		}

	}

}
